package com.java110.community.cmd.ownerRepair;

import com.alibaba.fastjson.JSONObject;
import com.java110.community.cmd.community.UpdateCommunityCmd;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairSettingDto;
import com.java110.dto.repair.RepairStaffsFlow;
import com.java110.dto.repair.RepairStateEnum;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.intf.community.IRepairSettingInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.Date;

/**
 * @author 陈立高
 * @date 2024/03/20
 */
@Java110Cmd(serviceCode = "ownerRepair.repairForceFinish")
public class RepairForceFinishCmd extends Cmd {
    private static Logger logger = LoggerFactory.getLogger(UpdateCommunityCmd.class);
    @Autowired
    private IRepairInnerServiceSMO repairInnerService;
    @Autowired
    private IRepairSettingInnerServiceSMO settingInnerServiceSMO;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {

    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        //强制回单
        logger.info("保存保修配置请求入参：{}", reqJson.toJSONString());
        Long repairId = reqJson.getLong("repairId");
        String webContext = reqJson.getString("context");
        //查询当前订单是否处于接单状态
        RepairDto repairDto = repairInnerService.getOneById(repairId);
        if (!repairDto.getState().equals("1100")) {
            throw new CmdException("当前订单状态不是接单状态，无法强制回单");
        }
        //获取上一次流转单。修改信息
        RepairStaffsFlow repairStaffsFlow = repairInnerService.lastTimeRepairFlow(repairId);
        repairStaffsFlow.setEndTime(DateUtil.getCurrentDate());
        repairStaffsFlow.setTimeConsuming(System.currentTimeMillis() - repairStaffsFlow.getCreatTime().getTime());
        repairInnerService.updateRepairDispatchFlow(repairStaffsFlow);


        //判断当前订单是否需要回访
        RepairSettingDto repairSettingDto = settingInnerServiceSMO.getOneById(repairDto.getRepairType());
        String returnVisitFlag = repairSettingDto.getReturnVisitFlag();
        Date now = DateUtil.getCurrentDate();
        if ("001".equals(returnVisitFlag)) {
            //不回访-》完成
            repairDto.setState(RepairStateEnum.COMPLETED.getState());
            repairDto.setStateName(RepairStateEnum.COMPLETED.getStateName());
        } else {
            //回访
            repairDto.setState(RepairStateEnum.RETURN_VISIT.getState());
            repairDto.setStateName(RepairStateEnum.RETURN_VISIT.getStateName());
        }
        JSONObject jsonObject = BeanConvertUtil.beanCovertJson(repairDto);
        jsonObject.put("appointmentTime", repairDto.getAppointmentTime());
        jsonObject.put("creatTime", repairDto.getCreatTime());
        //修改订单状态
        repairInnerService.updateOwnerRepair(jsonObject);
        //新增流转单
        //添加当前流转单
        repairStaffsFlow.setId(null);
        repairStaffsFlow.setCreatTime(DateUtil.getCurrentDate());
        repairStaffsFlow.setState(Integer.parseInt(repairDto.getState()));
        repairStaffsFlow.setStateName(repairDto.getStateName());
        repairStaffsFlow.setContext(webContext);
        JSONObject beanedCovertJson = null;
        if (!repairDto.getState().equals("1600")) {
            repairStaffsFlow.setEndTime(null);
            repairStaffsFlow.setTimeConsuming(null);
            beanedCovertJson = BeanConvertUtil.beanCovertJson(repairStaffsFlow);
        } else {
            beanedCovertJson = BeanConvertUtil.beanCovertJson(repairStaffsFlow);
            beanedCovertJson.put("appointmentTime",repairDto.getAppointmentTime());
            beanedCovertJson.put("endTime",now);
        }

        beanedCovertJson.put("createUserName", reqJson.get("userName"));
        beanedCovertJson.put("currentUserId", reqJson.get("userId"));
        repairInnerService.repairDispatchFlow(beanedCovertJson);

    }
}
